package com.koolearn.lms.core;

import com.koolearn.lms.dao.BaseDao;
import com.koolearn.lms.util.ExcelWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by hanchao on 2016/9/6.
 */
@Component
public class ExcelGenerator {
    @Autowired
    private BaseDao dao;

    public void generate(String sql){
        generate(sql,new File("result.xls"));
    }

    public void generate(String sql,File file){
        generate(sql,file);
    }
    public void generate(String sql , File file,DataTransform transformer){
        List<Map<String,Object>> result = dao.find(sql);
        if(transformer != null){
            result = transformer.transform(result);
        }
        ExcelWriter.write(file,result);
    }
}
